<template>
    <a-form ref='formRef' :label-col='{span : 5}' :model='formData'>
        <a-form-item style='display:none' name='id'>
            <a-input type='hidden' v-model:value='formData.id' />
        </a-form-item>
        <a-form-item v-if="!formData.id" label='账号' name='account' :rules="[{required : true, message : '请填写账号'}]">
            <a-input type='text' v-model:value='formData.account' :maxlength="30" show-count />
        </a-form-item>
        <a-form-item label='姓名' name='name' :rules="[{required : true, message : '请填写姓名'}]">
            <a-input type='text' v-model:value='formData.name' :maxlength="10" show-count />
        </a-form-item>
        <template v-if="formData.id == 0">
            <a-form-item label='密码' name='password' :rules="[{required : false, validator : pwdValidator}]">
                <a-input-password v-model:value='formData.password' :maxlength="30" show-count />
            </a-form-item>
        </template>
        <template v-else>
            <a-form-item label='密码' name='password' :rules="[{required : false, validator : pwdValidator}]">
                <a-input type='text' v-model:value='formData.password' :maxlength="30" show-count placeholder="不输入代表不修改原密码" />
            </a-form-item>
        </template>
        <a-form-item label='角色' name='roles'>
            <a-select v-model:value="formData.roles" :options="roleOptions"  mode="multiple" style="width: 100%" :field-names="{ label: 'name', value: 'id'}" />
        </a-form-item>
        <a-form-item label='手机号码' name='tel'>
            <a-input type='text' v-model:value='formData.tel' :maxlength="11" show-count />
        </a-form-item>
        <a-form-item label='备注' name='remark'>
            <a-input type='text' v-model:value='formData.remark' :maxlength="100" show-count />
        </a-form-item>
        <a-form-item label='启用' name='enable'>
            <a-switch v-model:checked='formData.enable' checked-children='启用' un-checked-children='禁用'/>
        </a-form-item>
    </a-form>
</template>

<script lang="ts" setup>
    import { saveApi } from '@/api/system/Member';
    import { optionsApi as roleListApi} from '@/api/system/Role'
import { isValidMobileNumber, recursionFn } from '@/utils';
import { Rule } from 'ant-design-vue/es/form';
    import { ref } from 'vue';


    const props = defineProps<{
        row? : any
    }>()


    const formRef = ref()
    const depts = ref<any[]>([])
    let bakData:any = {}

    const roleOptions = ref<any[]>([])

    
    const formData = ref({
        id : 0,
        account : '',
        name : '',
        password : '',
        tel : '',
        roles : [],
        remark : '',
        enable : true

    })

    const phoneRules = [
        {
            required : false,
            validator : async (_rule: Rule, value: string) => {
                if(bakData.id != undefined) { //编辑的
                    if(bakData.phone == formData.value.phone) {
                        return Promise.resolve();
                    }
                }

                if(value !== '' && !isValidMobileNumber(value)) {
                    return Promise.reject("手机号码不合法");
                }
                return Promise.resolve();
            }
        }
    ]

    const pwdValidator = async (_rule: Rule, value: string) => {
        
        if(value.length > 0) {
            if(value.length < 6) {
                return Promise.reject("密码长度至少6位");
            }
        }

    
        return Promise.resolve()
    }

    if(props.row) {
        bakData = {...props.row}
        for(let k in props.row) {
            if(props.row[k] !== null) {
                if(k in formData.value) {
                    if(k == 'deptId') {
                        formData.value[k] = props.row[k] || ''
                    }else {
                        formData.value[k] = props.row[k]
                    }
                    
                }
            }
            
        }
    }


    async function submit() {
        let params  = await formRef.value.validate()
        if(params.password === '') {
            delete params.password
        }

        return saveApi(params)
    }

    defineExpose({
        submit
    })

    // deptApi().then(r => {
    //     depts.value = recursionFn(r.data, 0, 'id', 'parentId', 'children')
    // })

    roleListApi().then(res => {
        if(res.data) {
            roleOptions.value = res.data
        }
        
    })
</script>